package 强化训练;

public class day23摩尔投票 {
    public static void main(String[] args) {
        int[] arr= {1,1,2,2,2,3,2};
        System.out.println(getValue(arr,7));
    }
    public static   int getValue(int[] gifts, int n) {
        int point = gifts[0];
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (count == 0){
                point = gifts[i];
            }
            if (point == gifts[i]){
                count++;
            }else {
                count--;
            }
        }
        count = 0;
        for (int i:gifts) {
            if (point == i){
                count++;
            }
        }
        if (count > n / 2){
            return point;
        }
        return 0;
    }
}
